

** COGNITIVE MEASURES **

	foreach p in b e {

	// Cognitive Self Control

		// Arrows Games

			* Variables
				summ g4bm_`p' g4bs_`p' g4bms_`p' g4dm_`p' g4ds_`p' g4dms_`p' g4fm_`p' g4fs_`p' g4fms_`p' g4a_`p' g4c_`p' g4e_`p'

			* Number of sec to complete each arrows games

				gen A_1timesec_`p' = g4bm_`p'*60 + g4bs_`p' + g4bms_`p'/1000
					la var A_1timesec_`p' "Arrow 1: Time (Sec)"

				gen A_2timesec_`p' = g4dm_`p'*60 + g4ds_`p' + g4dms_`p'/1000
					la var A_2timesec_`p' "Arrow 2: Time (Sec)"

				gen A_3timesec_`p' = g4fm_`p'*60 + g4fs_`p' + g4fms_`p'/1000
					la var A_3timesec_`p' "Arrow 3: Time (Sec)"

			* Number of errors for each arrow games

				gen A_1error_`p' = g4a_`p'
					la var A_1error_`p' "Arrow 1: Errors"

				gen A_2error_`p' = g4c_`p'
					la var A_2error_`p' "Arrow 2: Errors"

				gen A_3error_`p' = g4e_`p'
					la var A_3error_`p' "Arrow 3: Errors"

			* Z-score (by phase and round)

				foreach v in A_1timesec A_2timesec A_3timesec A_1error A_2error A_3error {
					gen `v'score_`p'=.
					foreach phase in 1 2 3 {
						foreach round in 1 2 3 4 5 6 7 8 {
							egen `v'_temp = std(`v'_`p') if p`phase'==1 & r`round'==1
							replace `v'score_`p' = `v'_temp if p`phase'==1 & r`round'==1
							drop `v'_temp
						}
					}
				}

			* Arrow baseline, inhibition and switching

				* Arrow baseline is z(seconds Arrow 1) + z(errors Arrow 1)
					gen arrow_baseline_`p' = A_1timesecscore_`p' + A_1errorscore_`p'

				* Arrow inhibition is [z(seconds Arrow 2) + z(errors Arrow 2)] �[ z(seconds Arrow 1) + z(errors Arrow 1)]
					gen arrow_inhibition_`p' = [A_2timesecscore_`p' + A_2errorscore_`p' ] - [A_1timesecscore_`p' + A_1errorscore_`p']

				* Arrow switching is [z(seconds Arrow 3 )+ z(errors Arrow 3)] �[ z(seconds Arrow 2) + z(errors Arrow 2)]
					gen arrow_switching_`p' = [A_3timesecscore_`p' + A_3errorscore_`p'] - [A_1timesecscore_`p' + A_1errorscore_`p' + A_2timesecscore_`p' + A_2errorscore_`p']

				* Make negative (so higher=better)
					foreach x in arrow_baseline_`p' arrow_inhibition_`p' arrow_switching_`p' {
						replace `x'=-`x'
					}

				* Z-scores (by phase and round)
				foreach v in arrow_baseline arrow_inhibition arrow_switching {
					gen `v'_score_`p'=.
					foreach phase in 1 2 3 {
						foreach round in 1 2 3 4 5 6 7 8{
							egen `v'_temp = std(`v'_`p') if p`phase'==1 & r`round'==1
							replace `v'_score_`p' = `v'_temp if p`phase'==1 & r`round'==1
							drop `v'_temp
							}
						}
					la var `v'_score_`p' "`v'_`p' Z-Score"
					}

			bysort phase round: summ arrow_baseline_score_`p' arrow_inhibition_score_`p' arrow_switching_score_`p'

	// PUZZLE

		* Seconds to do the puzzle

			gen g2cmtos_`p' = g2cm_`p'*60
			gen g2cmstos_`p' = g2cms_`p'/1000

			egen puzzletimesec_`p' = rowtotal (g2cmtos_`p' g2cs_`p' g2cmstos_`p')
			replace puzzletimesec_`p' = . if g2cm_`p' ==. & g2cs_`p' ==.
			/* Cap at 5 minutes */
			replace puzzletimesec_`p' = 300 if puzzletimesec_`p'>300 & !missing(puzzletimesec_`p')
			drop g2cmtos_`p' g2cmstos_`p'
				la var puzzletimesec_`p' "Puzzle: Time to finish the puzzle (sec)"

		* Puzzle score
			* (we give a penalty of 5 min to the people that finsish the puzzle before 5min but got it wrong.

			gen puzzletimesecpen_`p' = puzzletimesec_`p'
			replace puzzletimesecpen_`p' = puzzletimesecpen_`p' + 300 if puzzletimesec_`p'<300 & g2b_`p'==0
				la var puzzletimesecpen_`p' "Puzzle: Time to finish the puzzle (sec) (+penalty)"

	// MAZE

			egen mazetottime_`p' = rowtotal (g3m1esecstd_`p' g3m2esecstd_`p')
			replace mazetottime_`p' =. if g3m1esecstd_`p'==. & g3m2esecstd_`p'==.
				la var mazetottime_`p' "Maze: Total Time M1 and M2 (sec)"

	 // LITERACY
 		replace g6a_b=0 if g6a_b==. & g9a_b!=. & p3==1
		replace g6b_b=0 if g6b_b==. & g9a_b!=. & p3==1

		gen nbcorrectword_`p' = g6a_`p' - g6b_`p'
			la var nbcorrectword_`p' "Lit: Nb of correct words"

		gen literacy_`p' = 0 if nbcorrectword_`p' == 0
		replace literacy_`p' = 1 if nbcorrectword_`p' >0 & nbcorrectword_`p'<45
		replace literacy_`p' = 2 if nbcorrectword_`p' >=45 & nbcorrectword_`p'!=.
			la var literacy_`p'	"Lit: Literacy Index"

		gen highlit_`p' = (literacy_`p'==2) if literacy_`p'!=.
			la var highlit_`p' "Lit: 1 if high literate"
	}



** PLANNING **

	// Mazes
		* Endline p1_e5m p1_e7m p2_e2wks p2_e2w
		* BAseline p3_b p2_b p1_b

		foreach p in b e {
			gen maze3firsttouch_`p' = g3m3asec_`p'
				la var maze3firsttouch_`p' "Maze: Time to first touch M3"

			egen maze3firsttouchstd_`p' =std(maze3firsttouch_`p')

			gen maze1backtrack_`p' = g3m1c_`p'
				la var maze1backtrack_`p' "Maze: Nb of backtracks  M1"

			gen maze2backtrack_`p' = g3m2c_`p'
				la var maze2backtrack_`p' "Maze: Nb of backtracks  M2"

			gen maze3backtrack_`p' = g3m3c_`p'
				la var maze3backtrack_`p' "Maze: Nb of backtracks  M3"

			egen maze123backtrack_`p' = rowtotal(g3m1c_`p' g3m2c_`p' g3m3c_`p')
			replace maze123backtrack_`p' = . if g3m1c_`p' ==. & g3m2c_`p'==. & g3m3c_`p'==.
				la var maze123backtrack_`p' "Maze: Nb of backtracks  M1 M2 M3"

			foreach var in maze123backtrack maze1backtrack maze2backtrack maze3backtrack {
				egen `var'std_`p' =std(`var'_`p')
			}
		}

		/* Overall Cognitive Index */
		*****************************
			* Baseline
			* Z-scores
			foreach v in maze3backtrack maze3firsttouch 				///
			puzzletimesec A_1timesec A_2timesec A_3timesec A_1error 	///
			A_2error A_3error fordigscore backdigscore {
				qui sum `v'_b if round == 1, d
				qui gen `v'_med_b = cond(!missing(`v'_b),`v'_b ,r(p50))
				qui sum `v'_med_b if round == 1
				gen `v'_std_b = (`v'_med_b-r(mean))/r(sd)
				la var `v'_std_b "`v'_b Z-Score"
			}
			/* Rescale "bad" measures */
			foreach v in A_1timesec A_2timesec A_3timesec A_1error A_2error A_3error puzzletimesec maze3backtrack {
				gen `v'_std_resc_b = -`v'_std_b
				la var `v'_std_resc_b `"Resc: `: var la `v'_std_b'"'
			}
			// Overall Measure
			egen cognitive_score_all_b = rowmean(maze3backtrack_std_resc_b maze3firsttouch_std_b 	///
			puzzletimesec_std_resc_b A_1timesec_std_resc_b A_2timesec_std_resc_b 			///
			A_3timesec_std_resc_b A_1error_std_resc_b A_2error_std_resc_b A_3error_std_resc_b 	///
			fordigscore_std_b backdigscore_std_b)
				qui sum cognitive_score_all_b if round == 1
				qui replace cognitive_score_all_b = (cognitive_score_all_b - r(mean))/r(sd)
				la var cognitive_score_all_b "Standardized index of 11 cognitive function measures"

			// Cognitive Measure
			egen cognitive_score_b = rowmean(puzzletimesec_std_resc_b A_1timesec_std_resc_b 	///
			A_1error_std_resc_b fordigscore_std_b)
				qui sum cognitive_score_b if round == 1
				qui replace cognitive_score_b = (cognitive_score_b - r(mean))/r(sd)
				la var cognitive_score_b "Standardized index of 4 cognitive function measures"

		// Construct executive function measure
		*********************************
			foreach n in 2 3 {
				foreach A in timesec error {
					reg A_`n'`A'_std_b A_1`A'_std_b if round == 1
						predict A_`n'`A'_resid_b, resid
						qui sum A_`n'`A'_resid_b if round == 1,
						replace A_`n'`A'_resid_b = -(A_`n'`A'_resid_b - r(mean))/r(sd)
				}
			}
			reg backdigscore_std_b fordigscore_std_b if round == 1
				predict backdigscore_resid_b, resid
				qui sum backdigscore_resid_b if round == 1,
				replace backdigscore_resid_b = (backdigscore_resid_b - r(mean))/r(sd)
			egen ef_score_b = rowmean(A_2timesec_resid_b A_3timesec_resid_b A_2error_resid_b A_3error_resid_b maze3firsttouch_std_b maze3backtrack_std_resc_b backdigscore_resid_b)
				qui sum ef_score_b
				qui replace ef_score_b = (ef_score_b - r(mean))/r(sd)
				la var ef_score_b "Standardized index of executive function"

		// Cognitive abilities index, including literacy and numeracy
		foreach x in literacy_b mathscore_b cognitive_score_b ef_score_b {
			sum `x' if round == 1
			qui gen stdtemp_`x' = (`x' - r(mean))/r(sd)
		}
		egen cogabilities_z_b = rowmean(stdtemp_*)
		qui sum cogabilities_z_b if round == 1
		qui replace cogabilities_z_b = (cogabilities_z_b-r(mean))/r(sd)
		la var cogabilities_z_b "Index of literacy, numeracy, cognitive score, and executive function"
		drop stdtemp_*



		/* Overall Cognitive Index */
		*****************************
			* Baseline
			* Z-scores
			foreach v in maze3backtrack maze3firsttouch 				///
			puzzletimesec A_1timesec A_2timesec A_3timesec A_1error 	///
			A_2error A_3error fordigscore backdigscore {
				qui sum `v'_e if round == 1, d
				qui gen `v'_med_e = cond(!missing(`v'_e),`v'_e ,r(p50))
				qui sum `v'_med_e if round == 1
				gen `v'_std_e = (`v'_med_e-r(mean))/r(sd)
				la var `v'_std_e "`v'_e Z-Score"
			}
			/* Rescale "bad" measures */
			foreach v in A_1timesec A_2timesec A_3timesec A_1error A_2error A_3error puzzletimesec maze3backtrack {
				gen `v'_std_resc_e = -`v'_std_e
				la var `v'_std_resc_e `"Resc: `: variable label `v'_std_e'"'
			}
			// Overall Measure
			egen cognitive_score_all_e = rowmean(maze3backtrack_std_resc_e maze3firsttouch_std_e 	///
			puzzletimesec_std_resc_e A_1timesec_std_resc_e A_2timesec_std_resc_e 			///
			A_3timesec_std_resc_e A_1error_std_resc_e A_2error_std_resc_e A_3error_std_resc_e 	///
			fordigscore_std_e backdigscore_std_e)
				qui sum cognitive_score_all_e if round == 1
				qui replace cognitive_score_all_e = (cognitive_score_all_e - r(mean))/r(sd)
				la var cognitive_score_all_e "Standardized index of 11 cognitive function measures"

			// Cognitive Measure
			egen cognitive_score_e = rowmean(puzzletimesec_std_resc_e A_1timesec_std_resc_e 	///
			A_1error_std_resc_e fordigscore_std_e)
				qui sum cognitive_score_e if round == 1
				qui replace cognitive_score_e = (cognitive_score_e - r(mean))/r(sd)
				la var cognitive_score_e "Standardized index of 4 cognitive function measures"

		// Construct executive function measure
		*********************************
			foreach n in 2 3 {
				foreach A in timesec error {
					reg A_`n'`A'_std_e A_1`A'_std_e if round == 1
						predict A_`n'`A'_resid_e, resid
						qui sum A_`n'`A'_resid_e if round == 1,
						replace A_`n'`A'_resid_e = -(A_`n'`A'_resid_e - r(mean))/r(sd)
				}
			}
			reg backdigscore_std_e fordigscore_std_e if round == 1
				predict backdigscore_resid_e, resid
				qui sum backdigscore_resid_e if round == 1,
				replace backdigscore_resid_e = (backdigscore_resid_e - r(mean))/r(sd)
			egen ef_score_e = rowmean(A_2timesec_resid_e A_3timesec_resid_e A_2error_resid_e A_3error_resid_e  backdigscore_resid_e) /* maze3firsttouch_std_e maze3backtrack_std_resc_e */
				qui sum ef_score_e
				qui replace ef_score_e = (ef_score_e - r(mean))/r(sd)
				la var ef_score_e "Standardized index of executive function"

			* Generate exeuctive function components *
			egen ef_arrowtime_e = rowmean(A_2timesec_resid_e A_3timesec_resid_e)
			egen ef_arrowerror_e = rowmean(A_2error_resid_e A_3error_resid_e)
			egen ef_maze_e = rowmean(maze3firsttouch_std_e maze3backtrack_std_resc_e)
			gen ef_backdig_e = backdigscore_resid_e

			foreach q in arrowtime arrowerror maze backdig {
				qui sum ef_`q'_e
				replace ef_`q'_e = (ef_`q'_e - r(mean))/r(sd)
			}

		// Cognitive abilities index, including literacy and numeracy
		foreach x in literacy_e  cognitive_score_e ef_score_e {
			sum `x' if round == 1
			qui gen stdtemp_`x' = (`x' - r(mean))/r(sd)
		}
		egen cogabilities_z_e = rowmean(stdtemp_*)
		qui sum cogabilities_z_e if round == 1
		qui replace cogabilities_z_e = (cogabilities_z_e-r(mean))/r(sd)
		la var cogabilities_z_e "Index of literacy, numeracy, cognitive score, and executive function"
		drop stdtemp_*
